<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;
class TypeTable extends AbstractTable
{
    protected $table = 'hy_type';
    protected $id = 'type_id';
    /**
     * 获取首页类目信息
     * @param array $query
     */
    public function getTypeList($query = array())
    {
        $select = new Select($this->getTable());
        $select->order('sequence');
        $rowset = $this->selectWith($select);
        return $rowset->toArray();
    }
    /**
     * 删除首页类目信息
     */
    public function deleteType($query = array())
    {
        return $this->delete();
    }
    /**
     * 增加类目信息
     */
    public function addItem($data){
        
        $data = array(
            'type_name'=>$data['type_name'],
            'type_thumb_image'=>$data["type_thumb_image"],
            'type_Link'=>$data["type_Link"],
            'type_create_time'=>new Expression('NOW()'),
            'type_update_time'=>new Expression('NOW()'),
        );
        
        $this->insert($data);
        $id = $this->lastInsertValue;

    }
    
    public function updateItem($id,$data){
        if(empty($id)){
            throw new \Exception("id field is invalid!");
        }
        $item = array(
            'type_name'=>$data['type_name'],
            'type_Link'=>$data["type_Link"],
            'type_update_time'=>new Expression('NOW()'),
        );
        if(isset($data['type_thumb_image'])){
            $item['type_thumb_image'] = $data['type_thumb_image'];
        }
        return $this->update($item,array('type_id'=>$id));
    }
    
    public function deleteItem($id){
        if(empty($id)){
            throw new \Exception("id field is invalid!");
        }
        return $this->delete(array('type_id'=>$id));
    
    }
}